
export function addEvent (obj, type, callback, useCapture = false) {
  obj.addEventListener(type, callback, useCapture)
  addEventList(obj, type, callback, useCapture)
}

export function removeEvent (obj, type, callback, useCapture = false) {
  obj.removeEventListener(type, callback, useCapture)
  removeEventList(obj, type, callback, useCapture)
}

export function addEventList (obj, type, callback, useCapture = false) {
  if (obj.eventList) {
    if (obj.eventList[type]) {
      obj.eventList[type].push({ callback: callback, useCapture: useCapture })
    } else {
      obj.eventList[type] = [{ callback: callback, useCapture: useCapture }]
    }
  } else {
    obj.eventList = {}
    obj.eventList[type] = [{ callback: callback, useCapture: useCapture }]
  }
}

export function removeEventList (obj, type, callback, useCapture = false) {
  if (obj.eventList) {
    const eventList = obj.eventList
    if (eventList[type]) {
      for (let i = 0; i < eventList[type].length; i++) {
        if (eventList[type][i].callback === callback) {
          eventList[type].splice(i, 1)
          if (eventList[type].length === 0) {
            delete eventList[type]
          }
          break
        }
      }
    }
  }
}

export function hasEvent (obj, type, callback, useCapture = false) {
  let flag = false
  if (obj && obj.eventList) {
    const eventList = obj.eventList
    if (eventList[type]) {
      for (let i = 0; i < eventList[type].length; i++) {
        if (eventList[type][i].callback === callback) {
          flag = true
        }
      }
    }
  }
  return flag
}
